import {Module} from 'vuex'
import {CHG_LOGIN, UPDATE_USER_DETAIL, LOGIN_OUT} from '../mutation-types'
import { request } from '@/lib/http'
import { stat } from 'fs'
const tokenId = localStorage.getItem('token')
const openToken = tokenId || ''
// localStorage.setItem('openUserId', JSON.stringify({
//   OpenId: 'o6Crr0Ogy1CuCv8o1gMveHV', UserId: '11'
// }))
export const userInfo: Module<UserInfoState, any> = {
  state: {
    account: '',
    deleteFlag: false,
    factoryName: '',
    factoryid: '',
    flag: '',
    id: '',
    jobNum: '',
    name: '',
    phone: '',
    roleId: '',
    roleName: '',
    teamName: '',
    token: openToken,
    usedFlag: false,
    workGroupId: '',
    workGroupName: '',
  },
  mutations: {
    [CHG_LOGIN] (state, {key, value}) {
      Object.assign(state, {[key]: value})
    },
    [UPDATE_USER_DETAIL] (state, dataInfo) {
      Object.assign(state, dataInfo)
    },
    [LOGIN_OUT] (state) {
      state.token = ''
    },
  },
  actions: {
    get_login_info ({commit, state}) {
      return request({
        url: 'SGService/Login',
        data: {
        },
        method: 'get',
      }).then((userInfo:any) => {
        console.log('fetchUserInfo end')
        commit(UPDATE_USER_DETAIL, {...userInfo, isUserDetailFetched: true})
      })
    },
  },
}

export interface UserInfoState {
  account: string,
  deleteFlag: boolean,
  factoryName: string,
  factoryid: string,
  flag: string,
  id: string,
  jobNum: string,
  name: string,
  phone: string,
  roleId: string,
  roleName: string,
  teamName: string,
  token: string,
  usedFlag: boolean,
  workGroupId: string,
  workGroupName: string,
}
